//
//  SearchViewController.m
//  DialectShare
//
//  Created by timmy zhong on 4/4/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "SearchViewController.h"
#import "Const.h"
#import "SentenceDB.h"
#import "Dialect.h"
#import "DialectDB.h"
#import "ListenViewController.h"

@interface SearchViewController ()
@property (nonatomic, retain) NSArray *dialects;
@property (nonatomic, retain) DialectProxy *proxy;
@end

@implementation SearchViewController
@synthesize tableView = _tableView, dialects = _dialects, proxy = _proxy;
@synthesize searchBar = _searchBar;

- (void)dealloc
{
    self.tableView = nil;
    self.dialects = nil;
    self.proxy = nil;
    self.searchBar = nil;
    
    [super dealloc];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = @"搜索";
    
    _proxy = [[DialectProxy alloc] initWithDelegate:self];
    
    
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    [_proxy searchTerm:searchBar.text];
}

- (float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60.0f;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.dialects count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.searchBar resignFirstResponder];
    
    static NSString *identifier = @"identifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier] autorelease];
    }

    Dialect *dialect = [self.dialects objectAtIndex:indexPath.row];
    cell.textLabel.text = dialect.content;
    cell.detailTextLabel.text = dialect.yuxi;
    cell.detailTextLabel.font = [UIFont boldSystemFontOfSize:13];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    Dialect *dialect = [self.dialects objectAtIndex:indexPath.row];
    ListenViewController *controller = [[ListenViewController alloc] init];
    controller.dialect = dialect;
    [self.navigationController pushViewController:controller animated:YES];
    [controller release];
}


- (void)requestFailWithProxy:(Proxy *)proxy
{
    
}

- (void)requestFinishWithProxy:(Proxy *)proxy
{
    NSString *term = [self.searchBar text];
    NSString *condition = [NSString stringWithFormat:@"%@ like '%@%@%@'", DialectContent, @"%", term, @"%"];
    NSString *orderBy = [NSString stringWithFormat:@"%@ desc", DialectListen];
    DialectDB *db = [[DialectDB alloc] init];
    NSArray *dialects = [db getRecordsWithCondition:condition orderBy:orderBy start:0 count:1000];
    self.dialects = dialects;
    [db release];
    
    [self.tableView reloadData];
}










@end
